package com.smartstate.admin.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.smartstate.admin.biz.entity.dto.PermissionDto;
import com.smartstate.admin.biz.entity.dto.PermissionSearchDto;
import com.smartstate.admin.biz.entity.vo.PermissionVo;
import com.smartstate.admin.biz.service.OauthClientDetailsService;
import com.smartstate.admin.biz.service.PermissionService;
import com.smartstate.common.base.httpResult.ResultCode;
import com.smartstate.common.base.httpResult.ResultVO;
import com.smartstate.common.base.httpResult.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/authority")
@Api(tags = "权限管理")
public class PermissionController extends BaseController {


    @Resource
    private PermissionService permissionService;

    @Resource
    private OauthClientDetailsService oauthClientDetailsService;

    @GetMapping("/api/{id}")
    @ApiOperation(value = "根据用户id获取用户权限信息")
    public ResultVO getAuthorityByUserId(@PathVariable String id) {
        return ResultVO.success(permissionService.findPermissionByUserId(id));
    }

    @GetMapping("/api/info")
    @ApiOperation(value = "根据clientId获取认证客户端详情信息")
    public ResultVO getOauthClientDetailsByClientId(@RequestParam String clientId) {
        return ResultVO.success(oauthClientDetailsService.findOauthClientDetailsByClientId(clientId));
    }

    @GetMapping("/web/list")
    @ApiOperation(value = "查询权限管理列表--分页", notes = "查询权限管理列表--分页")
    public ResultVO getPermissionPage(@ModelAttribute PermissionSearchDto permissionSearchDto){
        return ResultVO.success(permissionService.queryPermissionList(permissionSearchDto));
    }

    @GetMapping("/web/tree")
    @ApiOperation(value = "查询权限树形结构--超级管理员可以查询所有企业的权限列表或者所有的权限--非超级管理员只能查询自己企业的权限列表" +
            "roleType : 1：读权限" +
            "2：写权限" +
            "3：读写权限(默认)", notes = "查询权限树形结构")
    public ResultVO getPermissionTree(@ModelAttribute PermissionSearchDto permissionSearchDto){
        return ResultVO.success(permissionService.queryPermissionTree(permissionSearchDto));
    }



    @GetMapping(value = "/web/info/{id}")
    @ApiOperation(value = "根据权限id--获取权限详情", notes = "根据权限id--获取权限详情")
    public ResultVO getPermission(@NotBlank @PathVariable String id){
        PermissionVo permission = permissionService.queryPermissionById(id);
        if (null == permission){
            return ResultVO.error("未查找到该权限");
        }else{
            return ResultVO.success(permission);
        }
    }

    @GetMapping("/web/user/{id}")
    @ApiOperation(value = "获取用户权限",notes = "获取用户权限")
    public ResultVO getPermissionByUserId(@ApiParam("用户id") @NotBlank @PathVariable String id) {
        return ResultVO.success(permissionService.getPermissionByUserId(id));
    }

    @GetMapping("/app/user/{id}")
    @ApiOperation(value = "获取用户权限",notes = "获取用户权限")
    public ResultVO getAppPermissionByUserId(@ApiParam("用户id") @NotBlank @PathVariable String id) {
        return ResultVO.success(permissionService.getAppPermissionByUserId(id));
    }


    @PostMapping(value = "/web/save")
    @ApiOperation(value = "新增权限管理-单个增加", notes = "新增权限管理-单个增加")
    public ResultVO savePermission(@RequestBody PermissionDto permissionDto){
        if (permissionService.saveOrUpdatePermission(permissionDto)){
            return ResultVO.success("保存成功");
        }else {
            return ResultVO.error("保存失败");
        }
    }
    @PostMapping(value = "/web/saveList")
    @ApiOperation(value = "新增权限管理-批量增加权限", notes = "新增权限管理-批量增加权限")
    public ResultVO savePermissionList(@RequestBody PermissionDto permissionDto){
        if (permissionService.saveOrUpdatePermission(permissionDto)){
            return ResultVO.success("保存成功");
        }else {
            return ResultVO.error("保存失败");
        }
    }

    @PutMapping(value = "/web/update")
    @ApiOperation(value = "修改权限管理", notes = "修改权限管理")
    public ResultVO updatePermission(@RequestBody PermissionDto permissionDto){
        if (permissionService.saveOrUpdatePermission(permissionDto)){
            return ResultVO.success("修改成功");
        }else {
            return ResultVO.error("修改失败");
        }
    }

    @DeleteMapping(value = "/web/del/{id}")
    @ApiOperation(value = "删除权限管理", notes = "删除权限管理")
    public ResultVO deletePermission(@NotBlank @PathVariable String id){
        if (permissionService.deletePermission(id)){
            return ResultVO.success("删除成功");
        }else {
            return ResultVO.error("删除失败");
        }
    }


    @DeleteMapping(value = "/web/delBatch")
    @ApiOperation(value = "批量删除权限", notes = "批量删除权限")
    public ResultVO deleteBatchPermission(@RequestBody @NotBlank  String ids){
        List<String> idList = JSON.parseArray(((JSONArray) JSON.parseObject(ids).get("ids")).toJSONString(), String.class);
        if (permissionService.deleteBatchPermission(idList)){
            return ResultVO.success("批量删除成功");
        }else {
            return ResultVO.error("批量删除失败");
        }
    }
    //--------------------------移动端-------------------------
    @GetMapping("/app/tree")
    @ApiOperation(value = "查询权限树形结构--超级管理员可以查询所有企业的权限列表或者所有的权限--非超级管理员只能查询自己企业的权限列表" +
            "roleType : 1：读权限" +
            "2：写权限" +
            "3：读写权限(默认)", notes = "查询权限树形结构")
    public ResultVO getAppPermissionTree(@ModelAttribute PermissionSearchDto permissionSearchDto){
        return ResultVO.success(permissionService.queryAppPermissionTree(permissionSearchDto));
    }
}
